'''
Created on Jul 31, 2011

@author: kafkef
'''


from PyQt4.QtGui import *
from PyQt4.QtCore import (QObject, SIGNAL, QAbstractTableModel, Qt, QModelIndex,
                          QVariant)

from dexen.system.client.ui.dialogs.export_dlg import ExportDialog

#===============================================================================
# Base Model Class
#===============================================================================
class BaseJobTableModel(QAbstractTableModel):
    def __init__(self, header_items, content_items, content_str, parent=None):
        QAbstractTableModel.__init__(self, parent)
        self._header = header_items
        self._content_items = content_items
        self._content_str = content_str
    
    def rowCount(self, index=QModelIndex()):
        return len(self._content_items)
    
    def columnCount(self, index=QModelIndex()):
        if len(self._content_items) == 0:
            return 0
        return len(self._content_items[0])

    def data(self, index, role):
        assert isinstance(index, QModelIndex)
        if not index.isValid(): return QVariant()
        
        if role == Qt.DisplayRole:
            return QVariant(self._content_items[index.row()][index.column()])
    
    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role != Qt.DisplayRole or self._header == []:
            return QVariant()
        if orientation == Qt.Horizontal:
            return QVariant(self._header[section])
    
    def get_log(self):
        return self._content_str    

#===============================================================================
# Base Job Page
#===============================================================================
class BaseJobPage(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self._create_widgets()
        self._models = {} #job_id: BaseJobTableModel
        self._cur_job_id = None
        
    def _create_widgets(self):
        # Create widgets
        self._table_view = QTableView()
        self._step_size_label = QLabel("Step size:")
        self._step_size_spinbox = QSpinBox()
        self._export_button = QPushButton("Export")
        
        # Configure widgets
        self._table_view.resizeColumnsToContents()
        self._step_size_spinbox.setValue(10)
        
        # Layout widgets
        h_layout = QHBoxLayout()
        from dexen.system.client.ui.pages.job_progress_page import JobProgressPage
        from dexen.system.client.ui.pages.job_advance_page import JobAdvancePage
        if isinstance(self, JobProgressPage) or isinstance(self, JobAdvancePage):
            h_layout.addWidget(self._step_size_label)
            h_layout.addWidget(self._step_size_spinbox)
        h_layout.addStretch()
        h_layout.addWidget(self._export_button)
        
        layout = QVBoxLayout()       
        layout.addLayout(h_layout)
        layout.addWidget(self._table_view)
        self.setLayout(layout)
        
        # Configure the signal/slot connections
        self.connect(self._export_button, SIGNAL("clicked()"), self.on_export)

    def update_cur_job_id(self, job_id):
        self._cur_job_id = job_id
        if not self._models.has_key(job_id):
            self.on_refresh()
        else:
            self._table_view.setModel(self._models[job_id])        
            #self._table_view.resizeColumnsToContents()     
    
    def on_refresh(self):
        # To be implemented by subclass
        pass
    
    def on_export(self):
        model = self._models[self._cur_job_id]
        assert isinstance(model, BaseJobTableModel)
        content_log = model.get_log()
        dlg = ExportDialog(self)
        ret_val = dlg.exec_()
        if ret_val == QDialog.Accepted:
            file_path = dlg.get_file_path()
            f = open(file_path, "wb")
            f.write(content_log)
            f.close()

            